package com.hgshop.yudongyang.controller;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.hgshop.yudongyang.domain.Spec;
import com.hgshop.yudongyang.domain.SpecOption;
import com.hgshop.yudongyang.service.SpecService;


@Controller
@RequestMapping("spec")
public class SpecController {

	
	@Reference
	SpecService specService;
	
	@RequestMapping("list")
	public String specList(Model model,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize) {
		
		//System.out.println("123456------------789");
		PageInfo<Spec> info = specService.specList(pageNum,pageSize);
		List<Spec> list = info.getList();
		
		model.addAttribute("info", info);
		return "spec/list";
	}
	@ResponseBody
	@PostMapping("dels")
	public boolean specdels(@RequestParam("ids[]")Integer[] ids) {
		int i = specService.dels(ids);
		
		return i>0;
	}
	@GetMapping("add")
	public String add() {
		
		return "spec/add";
	}
	@ResponseBody
	@PostMapping("add")
	public boolean add(Spec spec) {
		
		int i = specService.addSpec(spec);
		return i>0;
	}
	@ResponseBody
	@PostMapping("del")
	public boolean del(Integer id) {
		int i = specService.delSpec(id);
		
		return i>0;
	}
	/**
	 * 
	 * @Title: update 
	 * @Description:去修改
	 * @return
	 * @return: String
	 */
	@GetMapping("update")
	public String update(Integer id,Model model) {
		Spec spec = specService.getOne(id);//通过id获取一个spec对象
		model.addAttribute("spec", spec);
		return "spec/update";
	}
	@ResponseBody
	@PostMapping("update")
	public boolean update(Spec spec) {
		try {
			specService.update(spec);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
		
		
		
	}
	
	@PostMapping("findSpecOptionById")
	@ResponseBody
	public List<SpecOption> findSpecOptionById(Integer specId){
		
		List<SpecOption> list = specService.findSpecOptionById(specId);
		
		return list;
	}
}
